package com.archive.forum.controller;

import com.archive.forum.dto.PaginationDTO;
import com.archive.forum.model.User;
import com.archive.forum.service.CommentService;
import com.archive.forum.service.NotificationService;
import com.archive.forum.service.PostService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ProfileController {
    @Autowired
    private PostService postService;
    @Autowired
    private NotificationService notificationService;
    @Autowired
    private CommentService commentService;
    //个人页面
    @GetMapping("/profile/{action}")
    public String profile(@PathVariable(name = "action") String action, Model model, HttpServletRequest request,
                          @RequestParam(name = "page",defaultValue = "1") Integer page,
                          @RequestParam(name = "size",defaultValue = "5") Integer size) {

        User user =  (User) request.getSession().getAttribute("user");
        if(user == null){
            return "redirect:/";
        }
        switch (action) {
            case "index" -> {
                PaginationDTO paginationDTO= postService.listByUser(user.getId(),page,size);
                model.addAttribute("section", "index");
                model.addAttribute("sectionName", "我的发帖");
                model.addAttribute("paginationDTO",paginationDTO);
            }
            case "comment" -> {
                PaginationDTO paginationDTO= commentService.list(user.getId(),page,size);
                model.addAttribute("section", "comment");
                model.addAttribute("sectionName", "我的回复");
                model.addAttribute("paginationDTO",paginationDTO);
            }
            case "replies" -> {
                PaginationDTO paginationDTO= notificationService.list(user.getId(),page,10);
                model.addAttribute("section", "replies");
                model.addAttribute("sectionName", "我的通知");
                model.addAttribute("paginationDTO",paginationDTO);
            }
        }

        return "profile";
    }
    @GetMapping("/profile/")
    public String profile(){
        return "redirect:/profile/index";
    }
}
